import collect from 'collect.js';
import { defineStore } from 'pinia'
export const useRegisterStore = defineStore('register', {
  state: () => {
    return {
      row:{
        mobile_phone: '',
        email: '',
        verify: '',
        message_code: '',
        agree: 0,
        captcha_key:'',
        email:'',
        password_confirmation:''
      },
      loading:false,
      error:null,
      loaded:false,
      contactType:'',
      countdown:0,
      timer:null
    }
  },
  getters: {
    
  },
  actions: {
     //获取数据
     async fetchData() {
      this.loading = true;
      this.error = null;
      this.loaded = false;
      try {
        let res = await this.$request.get('/open/register');
        this.row = collect(res.data.row).merge(this.row);
        this.loaded = true;
      } catch (error) {
        this.error = error;
      }
      this.loading = false;
     },
     goToStep($router,step) {
      let path = '/guest/register';
      if(step){
        path += '/step'+step;
      }
      $router.push({path:path}); 
    },
    startCountdown(countdown){
      this.countdown = countdown;
      this.timer = setInterval(() => {
        this.countdown--;
        if(this.countdown<=0){
          clearInterval(this.timer);
        }
      }, 1000);
    }


  }
})